﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using _430GroupProject;
using System.Runtime.Serialization;
using _430GroupProject.Messaging;

namespace Model
{
    //TimeSheet for a time range and set of worksessions.
    [DataContractAttribute]
    public class TimeSheet : SuperClass
    {
        //Unique identifier for the timesheet used by system.
        [DataMember]
        public Guid TimeSheetID { get; private set; }

        [DataMember]
        public User User { get; private set; }

        [DataMember]
        public DateTime Start { get; set; }

        [DataMember]
        public DateTime End { get; set; }

        [DataMember]
        public IEnumerable<WorkSession> WorkSessions { get; set; }

        [DataMember]
        public WorkGroup Group { get; private set; }

        //Indicates if the timesheet has been approved by supervisor yet.
        [DataMember]
        private bool approvedBySupervisor;

        //Indicates if the timesheet has been approved by payroll yet.
        [DataMember]
        private bool approvedByPayroll;

        public TimeSheet(User user, WorkGroup grp, DateTime start, DateTime end) {
            this.TimeSheetID = Guid.NewGuid();
            this.User = user;
            this.WorkSessions = new List<WorkSession>();
            this.Group = grp;
            this.Start = start;
            this.End = end;
            approvedByPayroll = false;
            approvedBySupervisor = false;
        }

        public ErrorMessage submit() {
            EMSServices services = new EMSServices();
            User supervisor = services.getUser(this.Group.SupervisorID);
            if (supervisor.Alerts.Contains(Alert.TimeSheetSubmitted)) {
                Message message = new Message("System Alert", supervisor.NetID, User.FullName + " has submitted a timesheet for approval.", "TimeSheet Approval Notice");
                return MessageService.sendMessage(message);
            }
            return supervisor.Error;
        }

        public void supervisorApprove() {
            EMSServices services = new EMSServices();
            this.approvedBySupervisor = true;
            User usr = services.getUser(this.Group.PayrollID);
            if (usr.Alerts.Contains(Alert.TimeSheetMarkedForPayroll)) {
                Message message = new Message("System Alert", User.NetID, User.FullName + "'s Timesheet has been approved by supervisor and is ready for review.", "TimeSheet Approval Notice");
            }
        }

        public void payrollApprove(){
            if (!this.approvedBySupervisor)
            {
                throw new Exception("Timesheet as never approved by supervisor, can't be approved by payroll.");
            }
            this.approvedByPayroll = true;
        }

        public bool isApprovedBySupervisor() {
            return this.approvedBySupervisor;
        }

        public bool isApprovedByPayroll() {
            return this.approvedByPayroll;
        }

    }
}
